package com.shiroexploit.gui;

import com.shiroexploit.task.SendURLDNSPayloadTask;
import com.shiroexploit.util.Config;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class FuzzingKeyPange {
    private TextField dnsUrlText = new TextField();
    private TextField keyText = new TextField();
    private Button cancelButton = new Button();
    private Button confirmButton = new Button();
    private BorderPane borderPane = new BorderPane();
    private Button executeButton = new Button();
    private Config config = Config.getInstance();
    private Stage previousStage;
    private boolean sendPayload = false;

    public FuzzingKeyPange(Stage stage){
        drawPane();
        addEventListeners();
        this.previousStage = stage;
    }

    private void addEventListeners() {
        executeButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                String dnsUrl = dnsUrlText.getText().trim();
                if(dnsUrl == null || dnsUrl.equals("")){
                    PromptMessageUI.getAlert("输入错误","请输入一个 DNSLog 地址");
                    return;
                }

                SendURLDNSPayloadTask task = new SendURLDNSPayloadTask(dnsUrl);
                task.valueProperty().addListener(new ChangeListener<Integer>() {
                    @Override
                    public void changed(ObservableValue<? extends Integer> observable, Integer oldValue, Integer newValue) {
                        if(task.getStatus() == 1){
                            sendPayload = true;
                            PromptMessageUI.getAlert("成功","URLDNS 载荷发送完毕，请您从 DNSLog 平台查看结果");
                        }
                    }
                });

                Stage currentStage = (Stage) borderPane.getScene().getWindow();
                PenddingUI penddingUI = new PenddingUI(task, currentStage);
                penddingUI.activateProgressBar();
            }
        });

        cancelButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Stage currentStage = (Stage) borderPane.getScene().getWindow();

                currentStage.fireEvent(
                        new WindowEvent(
                                currentStage, WindowEvent.WINDOW_CLOSE_REQUEST
                        )
                );
                currentStage.close();
            }
        });

        confirmButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                String num = keyText.getText().trim();

                if(num == null || num.equals("")){
                    PromptMessageUI.getAlert("输入错误","您尚未输入 Key 序号");
                    return;
                }

                int seq;
                try{
                     seq = Integer.parseInt(num);
                }catch(NumberFormatException e){
                    PromptMessageUI.getAlert("输入错误","您输入的不是一个有效的数字");
                    return;
                }

                if(seq < 1 || seq > Config.getInstance().getKeys().size()){
                    PromptMessageUI.getAlert("输入错误","您输入的 Key 的序号不正确");
                    return;
                }

                if(!sendPayload){
                    PromptMessageUI.getAlert("错误","您尚未发送过 URLDNS 载荷");
                    return;
                }

                seq = seq - 1;
                String key = Config.getInstance().getKeys().get(seq);
                Config.getInstance().getKeys().clear();
                Config.getInstance().getKeys().add(key);

                Stage currentStage = (Stage) borderPane.getScene().getWindow();

                currentStage.fireEvent(
                        new WindowEvent(
                                currentStage, WindowEvent.WINDOW_CLOSE_REQUEST
                        )
                );
                currentStage.close();
            }
        });


    }

    public Pane getPane(){
        return borderPane;
    }

    private void drawPane() {

        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10,20,10,20));
        gridPane.setHgap(20);
        gridPane.setVgap(20);
        gridPane.setAlignment(Pos.CENTER);

        Label label1 = new Label("DNSLog 地址");
        gridPane.add(label1, 0, 0);
        dnsUrlText.setPromptText("比如 nvr27l.dnslog.cn");
        gridPane.add(dnsUrlText,1,0);

        executeButton.setText("发送 URLDNS 载荷");
        gridPane.add(executeButton, 1,1);


        Label label2 = new Label("提示: 如果 DNSLog 收到的请求为 3.nvr27l.dnslog.cn, 那么 key 的序号即为 3");
        GridPane.setColumnSpan(label2,2);
        gridPane.add(label2,0,3);

        Label label3 = new Label("DNSLog收到的Key序号");
        gridPane.add(label3,0,4);
        gridPane.add(keyText, 1,4);

        HBox bottom = new HBox();
        bottom.setAlignment(Pos.CENTER);
        cancelButton.setText("取 消");
        confirmButton.setText("确 定");
        cancelButton.setPrefWidth(80);
        confirmButton.setPrefWidth(80);
        bottom.getChildren().addAll(confirmButton, cancelButton);
        bottom.setMargin(confirmButton, new Insets(0,15,0,0));

        VBox vbox = new VBox();
        vbox.getChildren().addAll(gridPane, bottom);
        vbox.setAlignment(Pos.CENTER);
        vbox.setSpacing(20);

        borderPane.setCenter(vbox);
    }
}